import top.malaoshi.data.*;
import top.malaoshi.util.Out;

public class StdOrderBy {

    static DataTable createTable(){
        DataTable dt = new DataTable("t_user");

        dt.columns().add("id");
        dt.columns().add("username", DataColumn.Type.TEXT);
        dt.columns().add("password", DataColumn.Type.TEXT);
        dt.columns().add("age", DataColumn.Type.INT);
        dt.columns().add("height", DataColumn.Type.DOUBLE);
        dt.columns().add("sex", DataColumn.Type.TEXT);


        dt.rows().add(new Object[]{1,"lilei","123456",null,1.83,"男"});
        DataRow drHmm = dt.rows().add(new Object[]{2, "韩梅梅", "666", 20,1.65,"女"});


        dt.rows().add(new Object[]{5,"张三","123456",20,1.83});
        dt.rows().add(new Object[]{6,"李四","123456",30,1.83,"男"});
        dt.rows().add(new Object[]{7,"杜子腾","123456",35,1.83,"男"});
        dt.rows().add(new Object[]{8,"史珍香","123456",28,1.68,"女"});
        dt.rows().add(new Object[]{9,"王五","123456",28,1.83,"男"});
        dt.rows().add(new Object[]{10,"乔峰","123456",28,1.73,"男"});
        dt.rows().add(new Object[]{11,"段誉","123456",30,1.93,"男"});
        dt.rows().add(new Object[]{11,"小龙女","123456",35,1.83,"女"});
        dt.rows().add(new Object[]{11,"王语嫣","123456",30,1.63,"女"});

        return dt;
    }
    // 一列
    public static void main1(String[] args) {

        DataTable dt = createTable();

        DataTable res = dt.orderBy("age", OrderBy.Sort.asc).getResult();

        printColumns(res);
        printDatas(res);
    }
    // 多列
    public static void main(String[] args) {

        DataTable dt = createTable();

        DataTable res = dt.orderBy("age", OrderBy.Sort.asc)
                .orderBy("height", OrderBy.Sort.desc)
                .getResult();

        printColumns(res);
        printDatas(res);
    }

    static void printColumns(DataTable dt){
        Out.println("所有列、类型");
        for(int i = 0,size = dt.columns().count();i<size;i++){
            DataColumn item = dt.columns().get(i);
            Out.println(item.getName(),item.getType());
        }
    }

    static void printDatas(DataTable dt){
        Out.println("所有的数据：");
        for(int i = 0,size = dt.rows().count(); i<size ; i++){
            DataRow row = dt.rows().get(i);

            for(int j = 0,count = dt.columns().count();j<count;j++) {
                Out.print("", row.getValue(j),",");
            }
            Out.println();
        }
    }
}
